package com.xing.lei.happy.thrid.flh.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA1 {
    private static final Logger logger = LoggerFactory.getLogger(MD5.class);
    private static String key = "winning.com.cn";

    /**
     * 字符串加密
     * @param str 加密前字符串
     * @return String 加密后字符串
     */
    public static String encrypt(String str) {
        Assert.notNull(str);
        StringBuffer hexString = new StringBuffer();
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
//            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(str.getBytes());
            byte[] hash = md.digest();
            for (int i = 0; i < hash.length; i++) {
                if ((0xff & hash[i]) < 0x10) {
                    hexString.append("0"
                            + Integer.toHexString((0xFF & hash[i])));
                } else {
                    hexString.append(Integer.toHexString(0xFF & hash[i]));
                }
            }
        } catch (NoSuchAlgorithmException e) {
            logger.error(e.getMessage());
        }
        return hexString.toString();
    }

    /**
     * 字符串加密
     *
     * @param str 加密前字符串
     * @return String 加密后字符串
     */
    public static String encryptKey(String str) {
        Assert.notNull(str);
        StringBuffer hexString = new StringBuffer();
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(str.getBytes());
            byte[] hash = md.digest(key.getBytes("UTF-8"));
            for (int i = 0; i < hash.length; i++) {
                if ((0xff & hash[i]) < 0x10) {
                    hexString.append("0"
                            + Integer.toHexString((0xFF & hash[i])));
                } else {
                    hexString.append(Integer.toHexString(0xFF & hash[i]));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hexString.toString();
    }

    public static String encrypt(String str, String algorithm, String secretKey, String encoding) {
        Assert.notNull(str);
        StringBuffer sb = new StringBuffer();
        encoding = StringUtils.isBlank(encoding) ? "UTF-8" : encoding;
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            md.update(str.getBytes(encoding));
            byte[] bytes = StringUtils.isBlank(secretKey) ? new byte[] {}
                    : secretKey.getBytes(encoding);
            byte[] hash = md.digest(bytes);
            for (int i = 0; i < hash.length; i++) {
                if ((0xff & hash[i]) < 0x10) {
                    sb.append("0" + Integer.toHexString((0xFF & hash[i])));
                } else {
                    sb.append(Integer.toHexString(0xFF & hash[i]));
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return sb.toString();
    }
}
